package cn.rydiy.rmf.common;

import java.util.ArrayList;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

/**
 * Splash Display Class. It will display splash with full screen and then quit
 * full screen.
 * 
 * @author Rydiy
 * @version 1.0.0
 * @Date 2012-07-29
 */
public class RmfSplash {
	private Activity mActivity; // MainActivity
	private int mDelayTime; // delay time
	private int mMainLayoutResID; // /the resource id of the main layout
	private int mSplashLayoutResID; // /the resource id of the splash layout
	private int mSplashImgViewResID; // /the resource id of the splash imageview
	private int mSplashImgResID; // the resource id of the splash image data
	private ArrayList<Integer> mGuideImgs; // imags for guide
	private Handler mHandler; // message handler
	private Resources mRes; // the resources object containing the image data

	/**
	 * Constructor
	 * 
	 * @param mainActivity
	 *            :Application's main acitivity
	 */
	public RmfSplash(Activity mainActivity) {
		this.mActivity = mainActivity;
	}

	/**
	 * Set splash settings
	 * @param res
	 *            :The resources object containing the image data
	 * @param splashImgResID
	 *            :The resource id of the image data
	 */
	public void setSplash(Resources res, int splashImgResID) {
		this.mRes = res;
		String packageName = mActivity.getPackageName();
		// get R.layout.main
		mMainLayoutResID = mRes.getIdentifier("main", "layout", packageName);
		setSplash(res, splashImgResID, mMainLayoutResID);
	}

	/**
	 * Set splash settings
	 * @param res
	 *            :The resources object containing the image data
	 * @param splashImgResID
	 *            :The resource id of the image data
	 * @param mainLayoutResID
	 *            :The resource id of the application's main layout
	 */
	public void setSplash(Resources res, int splashImgResID, int mainLayoutResID) {
		this.mRes = res;
		this.mSplashImgResID = splashImgResID;
		this.mMainLayoutResID = mainLayoutResID;

		String packageName = mActivity.getPackageName();
		// get R.layout.rmf_ly_splash
		mSplashLayoutResID = mRes.getIdentifier("rmf_ly_splash", "layout",
				packageName);
		// get R.id.rmf_id_splash_img
		mSplashImgViewResID = mRes.getIdentifier("rmf_id_splash_img", "id",
				packageName);
		// create Handler
		mHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				if (1 == msg.arg1) {
					Log.d("RMF", "RmfSplash recive handler");
					// quit full screen
					quiteFullScreen();
					// show guideView
					if (null == mGuideImgs) {
						mActivity.setContentView(mMainLayoutResID);
					} else {
						showGuide();
					}
				}
			}
		};
	}

	/**
	 * Show splash and guide
	 * @param delayTime
	 *            : Time for splash display
	 */
	public void show(int delayTime) {
		Log.d("RMF", "RmfSplash show()");
		this.mDelayTime = delayTime;
		// no title
		mActivity.requestWindowFeature(Window.FEATURE_NO_TITLE);
		// full screen
		mActivity.getWindow().setFlags(
				WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		// set splash layout
		mActivity.setContentView(mSplashLayoutResID);

		// load bitmap
		Bitmap bmp = BitmapFactory.decodeResource(mActivity.getResources(),
				mSplashImgResID);
		ImageView img = (ImageView) mActivity.findViewById(mSplashImgViewResID);
		img.setImageBitmap(bmp);

		// delay time
		new Thread() {
			public void run() {
				Message msg = Message.obtain(mHandler);
				msg.arg1 = 1;
				mHandler.sendMessageDelayed(msg, mDelayTime);
			};
		}.start();
	}

	/**
	 * Set images for guide
	 * @param guideImgs
	 *            :Images for guide
	 */
	public void setGuide(ArrayList<Integer> guideImgs) {
		mGuideImgs = guideImgs;
	}

	/**
	 * Quite full screen
	 */
	private void quiteFullScreen() {
		final WindowManager.LayoutParams attrs = mActivity.getWindow()
				.getAttributes();
		attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
		mActivity.getWindow().setAttributes(attrs);
		mActivity.getWindow().clearFlags(
				WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
	}

	/**
	 * Show guide view
	 */
	private void showGuide() {
		SharedPreferences appInfo = mActivity.getPreferences(0);
		Boolean first = appInfo.getBoolean("first", true);
		if (first) {
			// first time, show guide
			appInfo.edit().putBoolean("first", false).commit();
			RmfGuide rmfGuide = new RmfGuide(mGuideImgs, mActivity, mRes);
			rmfGuide.show();
		} else {
			// Not the first time, set main layout
			mActivity.setContentView(mMainLayoutResID);
		}
	}
}
